<template>
	<view class="page">

		<view style="margin-left: 20px;margin-right: 20px;">
			<text style="word-break: break-all;">{{ message }}</text>
		</view>

		<scroll-view class="border list" scroll-y="true">
			<view v-for="(item,index) in settingList" :key="index" class="list-item" @click="settingItemClick(index)">
				<text class="text">{{item.text}}</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	var ble;
	var systemInfo;
	var _this;
	export default {
		data() {
			return {
				title: 'Setting',
				message: "",
				settingList: [{
						text: "1.Add photo on"
					},
					{
						text: "2.Add photo off"
					},
					{
						text: "3.Start run"
					},
					{
						text: "4.Stop run"
					},
					{
						text: "5.Start measure heart rate"
					},
					{
						text: "6.Stop measure heart rate"
					},
					{
						text: "7.Start measure blood pressure"
					},
					{
						text: "8.Stop measure blood pressure"
					},
					{
						text: "9.Start measure blood oxygen"
					},
					{
						text: "10.Stop measure blood oxygen"
					},
					{
						text: "11.Start measure temperature"
					},
					{
						text: "12.Stop measure temperature"
					},
					{
						text: "13.Start measure pressure"
					},
					{
						text: "14.Stop measure pressure"
					},
					{
						text: "15.Start measure blood glucose"
					},
					{
						text: "16.Stop measure blood glucose"
					},
					{
						text: "17.Start measure hrv"
					},
					{
						text: "18.Stop measure hrv"
					}
				],
				deviceSportState: [{
					stop: 0
				}, {
					start: 1
				}, {
					pause: 2
				}, {
					continueSport: 3
				}],
				deviceSportType: [{
						none: 0
					},
					{
						run: 0x01
					},
					{
						swimming: 0x02
					},
					{
						riding: 0x03
					},
					{
						fitness: 0x04
					},
					{
						ropeskipping: 0x06
					},
					{
						playball: 0x07
					},
					{
						walk: 0x08
					},
					{
						badminton: 0x09
					},
					{
						football: 0x0A
					},
					{
						mountaineering: 0x0B
					},
					{
						pingPang: 0x0C
					},
					{
						freeMode: 0x0D
					},
					{
						indoorRunning: 0x0E
					},
					{
						outdoorRunning: 0x0F
					},
					{
						outdoorWalking: 0x10
					},
					{
						indoorWalking: 0x11
					},
					{
						runMode: 0x12
					},
					{
						indoorRiding: 0x13
					},
					{
						stepper: 0x14
					},
					{
						rowingMachine: 0x15
					},
					{
						realTimeMonitoring: 0x16
					},
					{
						situps: 0x17
					},
					{
						jumping: 0x18
					},
					{
						weightTraining: 0x19
					},
					{
						yoga: 0x1A
					},
					{
						onfoot: 0x1B
					},
					{
						volleyball: 0x1C
					},
					{
						kayak: 0x1D
					},
					{
						rollerSkating: 0x1E
					},
					{
						tennis: 0x1F
					},
					{
						golf: 0x20
					},
					{
						ellipticalMachine: 0x21
					},
					{
						dance: 0x22
					},
					{
						rockClimbing: 0x23
					},
					{
						aerobics: 0x24
					},
					{
						otherSports: 0x25
					}
				],

			};
		},

		methods: {
			onLoad() {
				_this = this;
				_this.isAndroidOrIOS();
				if (systemInfo == "android") {	
					ble = uni.requireNativePlugin('bluetooth-ycSdkPlugin');
				} else {
					ble = uni.requireNativePlugin('DCTestUniPlugin');
				}
				ble.initPlugin();
				var globalEvent = uni.requireNativePlugin('globalEvent');

				var eventName = "deviceMeasurementResult";

				if (systemInfo == "ios") {
					eventName = "YCDeviceControlTypeHealthDataMeasurementResult";

				}
				console.log("eventName:" + eventName);
				globalEvent.addEventListener(eventName, function(res) {
					console.log("测量" + JSON.stringify(res));

				});
			},
			isAndroidOrIOS() {
				let type = uni.getSystemInfoSync().platform
				switch (type) {
					case 'android':
						systemInfo = "android";
						break;
					case 'ios':
						systemInfo = "ios";
						break;
					default:
						systemInfo = "other";
						break;
				}
			},
			settingItemClick(index) {
				console.log("点击index:" + index + ",text:" + _this.settingList[index].text);
				uni.showLoading({
					title: '加载中'
				});
				switch (index) {
					case 0:
						ble.appControlTakePhoto(true, (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 1:
						ble.appControlTakePhoto(false, (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 2:

						ble.appControlSport([_this.deviceSportState[1].start, _this.deviceSportType[1].run], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "开始运动成功";
						});
						break;
					case 3:
				uni.hideLoading();
						ble.appControlSport([_this.deviceSportState[0].stop, _this.deviceSportType[1].run], (res) => {						
							console.log(res.data);
						});
						_this.message = "停止运动成功";
						break;
					case 4:
						ble.appControlMeasureHealthData([true, 0], (res) => {
							uni.hideLoading();
							console.log(res.code);
						});
						break;
					case 5:
						ble.appControlMeasureHealthData([false, 0], (res) => {
							uni.hideLoading();
							console.log(res.code);
							_this.message = "操作成功";
						});
						break;
					case 6:
						ble.appControlMeasureHealthData([true, 1], (res) => {
							uni.hideLoading();
							console.log(res.code);
							_this.message = "操作成功";
						});
						break;
					case 7:
						ble.appControlMeasureHealthData([false, 1], (res) => {
							uni.hideLoading();
							console.log(res.code);
							_this.message = "操作成功";
						});
						break;
					case 8:
						ble.appControlMeasureHealthData([true, 2], (res) => {
							uni.hideLoading();
							console.log(res.code);
							_this.message = "操作成功";
						});
						break;
					case 9:
						ble.appControlMeasureHealthData([false, 2], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 10:
						ble.appControlMeasureHealthData([true, 4], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 11:
						ble.appControlMeasureHealthData([false, 4], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 12:
						ble.appControlMeasureHealthData([true, 12], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 13:
						ble.appControlMeasureHealthData([false, 12], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 14:
						ble.appControlMeasureHealthData([true, 5], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 15:
						ble.appControlMeasureHealthData([false, 5], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 16:
						ble.appControlMeasureHealthData([true, 10], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					case 17:
						ble.appControlMeasureHealthData([false, 10], (res) => {
							uni.hideLoading();
							console.log(res.data);
							_this.message = "操作成功";
						});
						break;
					default:
						break;
				}
			}
		}
	}
</script>

<style lang="scss">
	.page {
		padding-top: 20px;
		padding-bottom: 20px;
	}

	.row {
		display: flex;
		flex-direction: row;
		align-content: flex-start;
		align-items: flex-start;

	}

	.list {
		margin-top: 100px;
		margin-bottom: 100px;

		.list-item {
			background-color: #f8f8f8;

			.text {
				height: 50px;
				line-height: 50px;
				margin-left: 20px;
				color: #003366;
			}
		}
	}

	.log {
		width: 87vw;
		font-size: 13px;
		color: #003366;

		.log-bottom {
			height: 10vw;
		}
	}
</style>